package com.shangcheng.controller;

import com.shangcheng.common.Result;
import com.shangcheng.entity.OrderVo;
import com.shangcheng.entity.TOrders;
import com.shangcheng.service.OrderServiceImpl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/orderapi")
public class OrderController {
    @Autowired
    private OrderServiceImpl orderService;
    @PostMapping("/orders")
    public Result<Integer> addOrder(@RequestBody OrderVo ordervo) {
        return Result.ok(orderService.newOrder(ordervo.getOrder(),ordervo.getDetails()));
    }
    // 获取订单列表
    @GetMapping("/orders")
    public Result<List<TOrders>> getOrders() {
        List<TOrders> orders = orderService.getOrders();
        return Result.ok(orders);
    }
    //根据顾客id查订单待
    @GetMapping("/orders/{customerId}")
    public Result<List<TOrders>> getOrdersByCustomerId(@PathVariable Integer customerId) {
        List<TOrders> orders = orderService.getOrdersById(customerId);
        return Result.ok(orders);
    }

    // 假支付接口（待支付→已支付）
    @PostMapping("/orders/{orderId}/fakePay")
    public Result<String> fakePayOrder(@PathVariable Integer orderId) {
        orderService.fakePayOrder(orderId);
        return Result.ok("支付成功");
    }

    // 取消订单接口（待支付→已取消）
    @PostMapping("/orders/{orderId}/cancel")
    public Result<String> cancelOrder(@PathVariable Integer orderId) {
        orderService.cancelOrder(orderId);
        return Result.ok("订单已取消");
    }

    // 申请退款接口（已支付→申请退款）
    @PostMapping("/orders/{orderId}/applyRefund")
    public Result<String> applyRefund(@PathVariable Integer orderId) {
        orderService.applyRefund(orderId);
        return Result.ok("退款申请已提交");
    }

    // 取消退款申请（申请退款→已支付）
    @PostMapping("/orders/{orderId}/cancelRefund")
    public Result<String> cancelRefund(@PathVariable Integer orderId) {
        orderService.cancelRefund(orderId);
        return Result.ok("取消退款申请成功");
    }

    // 确认收货（已发货→已完成）
    @PostMapping("/orders/{orderId}/confirmReceive")
    public Result<String> confirmReceive(@PathVariable Integer orderId) {
        orderService.confirmReceive(orderId);
        return Result.ok("确认收货成功");
    }

    // 标记已发货（已支付→已发货）
    @PostMapping("/orders/{orderId}/markShipped")
    public Result<String> markShipped(@PathVariable Integer orderId) {
        orderService.markShipped(orderId);
        return Result.ok("标记已发货成功");
    }

    //删除订单
    @DeleteMapping("/orders/{orderId}")
    public Result<String>deleteOrder(@PathVariable Integer orderId){
        orderService.deleteOrder(orderId);
        return Result.ok("订单删除成功");
    }

    // 带搜索条件的订单列表
    @GetMapping("/orders/search")
    public Result<List<TOrders>> getOrders(@RequestParam(required = false) Integer search) {
        List<TOrders> orders = orderService.getOrdersBySearch(search);
        return Result.ok(orders);
    }

}
